package com.hema.auction.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.apkfuns.logutils.LogUtils;
import com.hema.auction.http.HttpInfo;
import com.hema.auction.http.HttpTag;
import com.hema.auction.listener.RequestCallBack;
import com.hema.auction.utils.Constant;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 描述:
 * 作者:john
 */
public class BaseLayout extends LinearLayout implements RequestCallBack {


    public BaseLayout(Context context) {
        super(context);
    }

    public BaseLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onResponse(HttpTag httpTag, Call call, Response response) {
        if (response.isSuccessful()) {
            try {
                String result = response.body().string();
                HttpInfo httpInfo = new HttpInfo(httpTag, call, result);
                Message message = new Message();
                message.what = Constant.HTTP_TYPE_ONRESPONSE;
                message.obj = httpInfo;
                mHandler.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            HttpInfo httpInfo = new HttpInfo(httpTag, call, new IOException());
            Message message = new Message();
            message.what = Constant.HTTP_TYPE_ONFAILEURE;
            message.obj = httpInfo;
            mHandler.sendMessage(message);
        }
    }

    @Override
    public void onFailure(HttpTag eventCode, Call call, IOException e) {

    }

    @Override
    public void onRequestFinish(HttpTag eventCode) {
        Message message = new Message();
        message.what = Constant.HTTP_TYPE_ONFINISH;
        message.obj = eventCode;
        mHandler.sendMessage(message);
    }

    protected Handler getDefaultHandler() {
        return mHandler;
    }


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            if (msg.what == Constant.HTTP_TYPE_ONRESPONSE) {
                HttpInfo httpInfo = (HttpInfo) msg.obj;
                try {
                    LogUtils.d("------onSuccess =" + httpInfo.getResult());
                    onSuccess(httpInfo.getHttpTag(), httpInfo.getCall(), new JSONObject(httpInfo.getResult()));
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
            } else if (msg.what == Constant.HTTP_TYPE_ONFAILEURE) {
                HttpInfo httpInfo = (HttpInfo) msg.obj;
                onError(httpInfo.getHttpTag(), httpInfo.getCall(), httpInfo.getE());
            } else if (msg.what == Constant.HTTP_TYPE_ONFINISH) {
                onFinish((HttpTag) msg.obj);
            } else {
                onHandleMessage(msg);
            }
        }
    };

    protected void onHandleMessage(Message msg) {

    }

    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {

    }

    protected void onError(HttpTag httpTag, Call call, IOException e) {
        LogUtils.d("------onError = " + e.toString());
    }

    protected void onFinish(HttpTag httpTag) {

    }
}
